package com.farben.web.controller.utils.date;

import org.joda.time.LocalDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DateUtils {
  private static final Logger log = LoggerFactory.getLogger(DateUtils.class);

  public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
  public static final String YYYY_MM_DD = "yyyy-MM-dd";
  public static final String BEGIN = " 00:00:00";
  public static final String END = " 23:59:59";
  public static final int MIN = 0;
  public static final int MAX = 1;

  public static Map<String, Date> getThisWeekTimeRange() {
    return getTimeRange(0, 0);
  }

  public static Map<String, Date> getWeekTimeRange(int plus) {
    return getTimeRange(plus, 0);
  }

  public static Map<String, Date> getThisMothTimeRange() {
    return getTimeRange(0, 1);
  }

  public static Map<String, Date> getMonthTimeRange(int plus) {
    return getTimeRange(plus, 1);
  }

  public static Map<String, Date> getTimeRange(int plus, int timeType) {
    LocalDate now = LocalDate.now();
    //    一周的周一是第一天、周末是最后一天
    //    int dayOfWeek = now.getDayOfWeek().getValue();
    //    LocalDate begin =
    // timeType==0?now.minusDays(dayOfWeek-1).plusWeeks(plus):now.withDayOfMonth(1).plusMonths(plus);
    //    LocalDate end =
    // timeType==0?now.plusDays(7-dayOfWeek).plusWeeks(plus):now.withDayOfMonth(now.lengthOfMonth()).plusMonths(plus);

    //  一周的周末是第一天，周六是最后一天
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(LocalDate.now().toDate());
    int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

    LocalDate begin =
        timeType == 0
            ? now.minusDays(dayOfWeek - 1).plusWeeks(plus)
            : now.withDayOfMonth(1).plusMonths(plus);
    LocalDate end =
        timeType == 0
            ? now.plusDays(7 - dayOfWeek).plusWeeks(plus)
            : now.withDayOfMonth(java.time.LocalDate.now().lengthOfMonth()).plusMonths(plus);

    Map<String, Date> range = new HashMap<>();
    Date beginDate = convertBegin(begin.toDate());
    Date endDate = convertEnd(end.toDate());
    range.put("begin", beginDate);
    range.put("end", endDate);
    return range;
  }

  public static Date convertBegin(Date origin) {
    return convert(origin, BEGIN);
  }

  public static Date convertEnd(Date origin) {
    return convert(origin, END);
  }

  public static Date convert(Date origin, String time) {
    return convert(origin, time, YYYY_MM_DD_HH_MM_SS);
  }

  public static Date convert(Date origin, String time, String pattern) {
    SimpleDateFormat format = new SimpleDateFormat(pattern);
    Date date = null;
    try {
      date = format.parse(new LocalDate(origin) + time);
    } catch (ParseException p) {
      log.warn("get date exception:" + p);
    }
    return date;
  }

  public static Date firstWeekDay(int year, int dayOfWeek) {
    return weekDay(year, 1, dayOfWeek);
  }

  public static Date weekDay(int year, int month, int dayOfWeek) {
    Calendar calendar = Calendar.getInstance();
    calendar.setWeekDate(year, month, dayOfWeek);
    return DateUtils.convertBegin(calendar.getTime());
  }

  public static Date queryJanuaryBEDay(int year, int beginOrEnd) {
    return beginOrEnd == MIN
        ? DateUtils.convertBegin(
            LocalDate.parse(year + "-01-01").dayOfMonth().withMaximumValue().toDate())
        : DateUtils.convertEnd(
            LocalDate.parse(year + "-01-01").dayOfMonth().withMaximumValue().toDate());
  }

  public static Date queryMonthBEDay(int year, int month, int beginOrEnd) {
    return beginOrEnd == MIN
        ? DateUtils.convertBegin(
            LocalDate.parse(year + "-" + month + "-01").dayOfMonth().withMinimumValue().toDate())
        : DateUtils.convertEnd(
            LocalDate.parse(year + "-" + month + "-01").dayOfMonth().withMaximumValue().toDate());
  }
}
